package com.exam.classes.service;

import com.alibaba.fastjson.JSON;
import com.exam.classes.client.UserClient;
import com.exam.classes.mapper.ClassesMapper;
import com.exam.classes.mapper.StudentMapper;
import com.exam.classes.pojo.Classes;
import com.exam.classes.pojo.Student;
import com.exam.common.until.NumberUtils;
import com.exam.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : ClassesService
 * @Description : 班级
 * @Author : WHB
 */
@Service
public class ClassesService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private ClassesMapper classesMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 创建班级
     * @param tId
     * @param name
     */
    public void createClasses(Long tId, String name) {

        Classes classes = new Classes();
        classes.setTid(tId);

        //根据id查找老师名称
        classes.setTname(userClient.queryUserById(tId).getUsername());

        classes.setName(name);
        String code = NumberUtils.generateCode(6);
        classes.setJoinKey(code);
        Date date = new Date();
        classes.setCreatTime(new Date());
        classesMapper.insert(classes);
    }

    /**
     * 删除班级
     * @param id
     */
    public void deleteClasses(int id) {

        Classes classes = new Classes();
        classes.setId(id);
        classesMapper.delete(classes);
    }

    /**
     * 通过id获取班级信息
     * @param id
     */
    public void getClasses(int id) {
        Classes classes = new Classes();
        classes.setId(id);
        classesMapper.selectOne(classes);
    }

    /**
     * 通过joinKey查询班级id
     * @param joinKey
     * @return
     */
    public int queryClassesByKey(String joinKey) {
        Classes classes = new Classes();
        classes.setJoinKey(joinKey);
        System.out.println("查询班级id成功");
        int id = classesMapper.selectOne(classes).getId();
        System.out.println(id);
        return id;
    }

    /**
     * 通过学生ID查找班级列表
     * @param sid
     * @return
     */
    public List<Classes> queryClassesListBySid(Long sid) {
        Student student = new Student();
        student.setSid(sid);
        //找到该学生所属的全部班级
        List<Student> studentList = studentMapper.select(student);

        //构造返回列表
        List<Classes> classesList = new ArrayList<>();

        //遍历得到班级id
        for (Student s:
             studentList) {
            Classes classes = classesMapper.selectByPrimaryKey(s.getClassId());
            classesList.add(classes);
        }

        return classesList;
    }

    public List<Classes> queryClassesListByTid(Long tid) {
        Classes classes = new Classes();
        classes.setTid(tid);
        List<Classes> classesList = classesMapper.select(classes);
        return classesList;
    }

    public List<User> queryStuListByCid(Integer cid) {
        Student student = new Student();
        student.setClassId(cid);
        List<Student> studentList = studentMapper.select(student);

        //返回结果列表
        List<User> userList = new ArrayList<>();

        for (Student stu:
             studentList) {
            userList.add(userClient.queryUserById(stu.getSid()));
        }

        return userList;
    }
}
